/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.scriptmark.core.block;

import com.jspx.scriptmark.config.TemplateConfigurable;
import com.jspx.scriptmark.core.TagNode;
import com.jspx.scriptmark.core.TemplateElement;
import com.jspx.scriptmark.Configurable;
import com.jspx.utils.StringUtil;
import com.jspx.utils.XMLUtil;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2008-11-14
 * Time: 17:22:15
 * 列表循环
 * <#list v=list>
 * <li>${v.index}-${v}</li>
 * </#list>
 * or
 * <#list var=v list=listV>
  * <li>${v.index}-${v}</li>
  * </#list>
 * 
 */
public class ListBlock extends TagNode {
    public static final  Configurable config = TemplateConfigurable.getInstance().copy();
    public static final String var = "var";
    public static final String list = "list";
    public static final String equals_KEY = "equals";
    static
    {
        config.setTag(BreakBlock.name, BreakBlock.class.getName());
        config.setTag(ContinueBlock.name, ContinueBlock.class.getName());
    }

    public String getVarName() {
        String name = getAttribute(var);
        if (StringUtil.hasLength(name)) return name;
        String[] attributeNames = getAttributeName();
        //不为 list= 的就当成变量名
        if (attributeNames != null)
            for (String s : attributeNames) {
                if (!list.equalsIgnoreCase(s)) {
                    return s;
                }
            }
        return null;
    }

    public String getListName() {
        return XMLUtil.deleteQuote(getAttribute(getVarName()));
    }

    public boolean getEquals() {
        String eq = XMLUtil.deleteQuote(getAttribute(equals_KEY));
        return !("false".equalsIgnoreCase(eq) || "0".equals(eq));
    }

    public List<TagNode> getChildList() throws Exception {
        TemplateElement templateEl = new TemplateElement(getBody(),0, config);
        return templateEl.getRootTree();
    }
}